﻿using System.Windows.Forms;
using BasecampRestAPI;
using BasecampUI;

namespace SplitToDoList
{
	public partial class MainForm : Form, IMainForm
	{
		private readonly MainFormMediator _mediator;
		private readonly CredentialsMainFormMediator _credentialsMediator;

		public MainForm()
		{
			InitializeComponent();
			_mediator = MainFormMediator.GetInstance(this);
			_credentialsMediator  = CredentialsMainFormMediator.GetInstance(this);
		}

		#region Implementation of IMainForm

		public ICredentialsForm CredentialsForm
		{
			get { return new CredentialsForm(); }
		}

		public IBaseCamp GetBaseCamp(IRestWebService service)
		{
			return BaseCamp.GetInstance(service);
		}

		public bool GetBaseCampFromCredentials(out IBaseCamp camp)
		{
			return _credentialsMediator.GetBaseCampFromCredentials(out camp);
		}

		public int ProjectSelectedIndex
		{
			get { return sourceProjectComboBox.SelectedIndex; }
			set { sourceProjectComboBox.SelectedIndex = value; }
		}

		public int ToDoListSelectedIndex
		{
			get { return sourceListComboBox.SelectedIndex; }
			set { sourceListComboBox.SelectedIndex = value; }
		}

		public void AddProject(string name)
		{
			sourceProjectComboBox.Items.Add(name);
		}

		public void ClearToDoLists()
		{
			sourceListComboBox.Items.Clear();
		}

		public void ToDoListsAddItem(string item)
		{
			sourceListComboBox.Items.Add(item);
		}

		public void ClearToDoListItems()
		{
			newListItemsListBox.Items.Clear();
		}

		public void ToDoListItemsAddItem(string content)
		{
			newListItemsListBox.Items.Add(content);
		}

		public bool SplitButtonEnabled
		{
			set { splitButton.Enabled = value; }
		}

		public string NewListName
		{
			get { return newListNameTextBox.Text; }
		}

		public int[] SelectedListItems
		{
			get
			{
				int[] result = new int[newListItemsListBox.SelectedIndices.Count];
				newListItemsListBox.SelectedIndices.CopyTo(result, 0);
				return result;
			}
		}

		#endregion

		private void MainForm_Load(object sender, System.EventArgs e)
		{
			_mediator.Load();
		}

		private void sourceProjectComboBox_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			_mediator.SourceProjectSelectedIndexChanged();
		}

		private void sourceListComboBox_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			_mediator.SourceToDoListSelectedIndexChanged();
		}

		private void newListNameTextBox_TextChanged(object sender, System.EventArgs e)
		{
			_mediator.NewListNameChanged();
		}

		private void splitButton_Click(object sender, System.EventArgs e)
		{
			_mediator.SplitButtonClicked();
		}
	}
}
